package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.circle.CircleImageView;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;
import com.liuzhongni.wahahanews.topicclass.TopicDetailsSecond;

/**
 * Created by dllo on 16/1/26.
 */
public class ListViewAdapterOfTopicAnswer extends BaseAdapter{
    private TopicDetailsSecond data;
    private int type;
    private Context context;

    public ListViewAdapterOfTopicAnswer(TopicDetailsSecond data, int type, Context context) {
        this.data = data;
        this.type = type;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.getLatestList().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(type == 1){
            ViewHolder holder;
            if(convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.topic_question_answer_item,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(data.getLatestList().get(position).getQuestion().getUserHeadPicUrl()!=null){

                VolleySingleton.getOurInstance().imgJson(holder.imageViewQuestion,data.getLatestList().get(position).getQuestion().getUserHeadPicUrl());
            }else {
                holder.imageViewQuestion.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
            }
            holder.textViewQuestionName.setText(data.getLatestList().get(position).getQuestion().getUserName());
            holder.textViewQuestionContect.setText(data.getLatestList().get(position).getQuestion().getContent());
            VolleySingleton.getOurInstance().imgJson(holder.imageViewAnswer, data.getLatestList().get(position).getAnswer().getSpecialistHeadPicUrl());
            holder.textViewAnswerName.setText(data.getLatestList().get(position).getAnswer().getSpecialistName());
            holder.textViewAnswerContect.setText(data.getLatestList().get(position).getAnswer().getContent());
            holder.textViewAnswerTime.setText(data.getLatestList().get(position).getAnswer().getcTime()+"");
            holder.textViewsupportCount.setText(data.getLatestList().get(position).getAnswer().getSupportCount()+"");
            holder.textViewreplyCount.setText(data.getLatestList().get(position).getAnswer().getReplyCount()+"");
        }else if(type == 2){
            ViewHolder holder;
            if(convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.topic_question_answer_item,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(data.getHotList().get(position).getQuestion().getUserHeadPicUrl()!=null){

                VolleySingleton.getOurInstance().imgJson(holder.imageViewQuestion,data.getHotList().get(position).getQuestion().getUserHeadPicUrl());
            }else {
                holder.imageViewQuestion.setImageResource(R.mipmap.iconfont_jinxingzhongnew);
            }
            holder.textViewQuestionName.setText(data.getHotList().get(position).getQuestion().getUserName());
            holder.textViewQuestionContect.setText(data.getHotList().get(position).getQuestion().getContent());
            VolleySingleton.getOurInstance().imgJson(holder.imageViewAnswer,data.getHotList().get(position).getAnswer().getSpecialistHeadPicUrl());
            holder.textViewAnswerName.setText(data.getHotList().get(position).getAnswer().getSpecialistName());
            holder.textViewAnswerContect.setText(data.getHotList().get(position).getAnswer().getContent());
            holder.textViewAnswerTime.setText(data.getHotList().get(position).getAnswer().getcTime()+"");
            holder.textViewsupportCount.setText(data.getHotList().get(position).getAnswer().getSupportCount()+"");
            holder.textViewreplyCount.setText(data.getHotList().get(position).getAnswer().getReplyCount()+"");
        }

        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    class ViewHolder{
        private CircleImageView imageViewQuestion;
        private TextView textViewQuestionName;
        private TextView textViewQuestionContect;
        private CircleImageView imageViewAnswer;
        private TextView textViewAnswerName;
        private TextView textViewAnswerContect;
        private TextView textViewAnswerTime;
        private TextView textViewsupportCount;
        private TextView textViewreplyCount;
        private int position;

        public ViewHolder(View view) {
            imageViewQuestion = (CircleImageView) view.findViewById(R.id.topic_question_head_iv);
            textViewQuestionName = (TextView) view.findViewById(R.id.topic_question_name_tv);
            textViewQuestionContect = (TextView) view.findViewById(R.id.topic_question_content_tv);
            imageViewAnswer = (CircleImageView) view.findViewById(R.id.topic_answer_head_iv);
            textViewAnswerName = (TextView) view.findViewById(R.id.topic_answer_name_tv);
            textViewAnswerContect = (TextView) view.findViewById(R.id.topic_answer_content_tv);
            textViewAnswerTime = (TextView) view.findViewById(R.id.topic_question_answer_item_time);
            textViewsupportCount = (TextView) view.findViewById(R.id.topic_question_answer_item_supportCount);
            textViewreplyCount = (TextView) view.findViewById(R.id.topic_question_answer_item_replyCount);

        }
    }

    public void upData(){
        notifyDataSetChanged();
    }
}
